﻿using System;
using System.Text;
namespace Jumbot.Common
{
    /// <summary>
    /// 发送邮件类
    /// </summary>
    public static class Mail
    {
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="MailTo">接收人用户名</param>
        /// <param name="MailSubject">邮件主题</param>
        /// <param name="MailBody">邮件内容</param>
        /// <param name="IsHtml">邮件正文是否为HTML格式</param>
        /// <param name="MailFrom">发件人邮箱地址</param>
        /// <param name="MailFromName">发件人署名</param>
        /// <param name="MailPwd">发件人邮箱密码</param>
        /// <param name="MailFromHost">发件人邮箱Host,如"smtp.126.com"</param>
        public static bool Send(string MailTo, string MailSubject, string MailBody, bool IsHtml, string MailFrom, string MailFromName, string MailPwd, string MailFromHost)
        {
            try
            {
                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                message.From = new System.Net.Mail.MailAddress(MailFrom, MailFromName, Encoding.GetEncoding("utf-8"));
                message.To.Add(MailTo);
                message.Subject = MailSubject;
                message.Body = MailBody;
                message.IsBodyHtml = IsHtml;
            	message.Priority = System.Net.Mail.MailPriority.Normal;
            	message.BodyEncoding = Encoding.GetEncoding("utf-8");
                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(MailFromHost);
                smtp.Credentials = new System.Net.NetworkCredential(MailFrom, MailPwd);
                smtp.Send(message);
            }
            catch (FormatException)
            {
                return false;
            }
            return true;
        }
    }
}
